import { defineStore } from 'pinia';
import { getDepartments, getPositions, getAttendanceData, clockIn,updateAttendanceStatus,addAttendance } from '../../api/attendance';

export const useAttendanceStore = defineStore('attendance', {
  state: () => ({
    departments: [],
    positions: [],
    attendanceData: [],
    total: 0, // 总条数
    pagination: {
      currentPage: 1,
      pageSize: 10
    },
    errorVisible: false,
    errorMessage: ''
  }),
  actions: {
    async fetchDepartments() {
      try {
        const response = await getDepartments();
        console.log('Fetched response:', response.success);  // 打印部门数据
        if (response.success) {
          this.departments = response.data.department.map(dept => ({ label: dept, value: dept }));
        }
      } catch (error) {
        this.errorVisible = true;
        this.errorMessage = '获取部门数据失败';
      }
    },
    async fetchPositions() {
      try {
        const response = await getPositions();
        if (response.success) {
          this.positions = response.data.position.map(pos => ({ label: pos, value: pos }));
          console.log('Fetched Positions:', this.positions);  // 打印职位数据
        }
      } catch (error) {
        this.errorVisible = true;
        this.errorMessage = '获取职位数据失败';
        
      }
    },
    async fetchAttendanceData() {
      try {
        const response = await getAttendanceData(this.pagination.currentPage, this.pagination.pageSize);
        if (response.success) {
          this.attendanceData = response.data.check.map(item => ({
            id: item.id,
            date: item.date,
            employeeId: item.employeeCompany.id,
            name: item.employeeCompany.name,
            department: item.employeeCompany.department,
            position: item.employeeCompany.position,
            status: item.employeeCompany.status,
            attendanceStatus: item.status,
          }));
          this.total = response.data.total; // 设置总条数
        } else {
          this.errorVisible = true;
          this.errorMessage = response.message || '获取考勤数据失败';
        }
      } catch (error) {
        this.errorVisible = true;
        this.errorMessage = '获取考勤数据失败';
        console.error('Error fetching attendance data:', error);
      }
    },
    async performClockIn(ids) {
      try {
        const response = await clockIn(ids); // 调用 API 函数
        if (response.success) {
          this.fetchAttendanceData(); // 成功后重新获取考勤数据
        } else {
          throw new Error(response.message || '打卡失败');
        }
      } catch (error) {
        this.errorVisible = true;
        this.errorMessage = error.message || '打卡失败';
      }
    },
    async updateAttendanceStatus(attendanceId, newStatus) {
      try {
        const response = await updateAttendanceStatus(attendanceId, newStatus); // 确保这个函数已正确导入
        if (response.success) { // 直接访问 response.success
          await this.fetchAttendanceData(); // 更新成功后重新获取考勤数据
        } else {
          throw new Error(response.message || '更新考勤状态失败');
        }
      } catch (error) {
        this.errorVisible = true;
        this.errorMessage = error.message || '更新考勤状态失败';
      }
    },
    async addAttendanceRecords(attendanceRecords) {
      try {
        const response = await addAttendance(attendanceRecords);
        if (response.success) {
          this.fetchAttendanceData();
        } else {
          throw new Error(response.message || '添加考勤记录失败');
        }
      } catch (error) {
        this.errorVisible = true;
        this.errorMessage = error.message || '添加考勤记录失败';
      }
    }
  }
});